package com.example.sys.org.model;

import com.example.core.model.BaseModel;
import com.example.sys.org.form.SysOrgElementForm;
import com.example.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
@Entity
@Table(name = "sys_org_element")
@Inheritance(strategy = InheritanceType.JOINED)
public class SysOrgElement extends BaseModel<SysOrgElement> implements SysOrgConstant {

    @JsonIgnore
    @OneToOne
    @JoinColumn(name = "fd_parent_id", referencedColumnName = "fd_id")
    private SysOrgElement fdParent;

    @Column(name = "fd_order_num")
    private int fdOrderNum;

    @Column(name = "fd_org_type")
    protected int fdOrgType = SysOrgConstant.ORG;

    @Column(name = "fd_hierarchy_id")
    private String fdHierarchyId;

    private transient List<SysOrgElement> children = new ArrayList();

    @Override
    public void recalculateFields() {
        super.recalculateFields();
        if (StringUtils.isEmpty(this.fdHierarchyId)) {
            if (this.fdParent == null) {
                this.fdHierarchyId = PREF + this.fdId;
            } else {
                this.fdHierarchyId = this.fdParent.getFdHierarchyId() + PREF + this.fdId;
            }
        }
    }

    @Override
    public Class<?> getFormClass() {
        return SysOrgElementForm.class;
    }
}
